package com.google.android.material.internal;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewGroup.MarginLayoutParams;
import androidx.annotation.RestrictTo;
import androidx.annotation.RestrictTo.d;
import androidx.core.view.ViewCompat;
import o.aqu.n;
import o.hi;

@RestrictTo({d.LIBRARY_GROUP})
public class FlowLayout extends ViewGroup {
    private boolean ˊ;
    private int ˋ;
    private int ˎ;

    public FlowLayout(Context context) {
        this(context, null);
    }

    public FlowLayout(Context context, AttributeSet attributeSet) {
        this(context, attributeSet, 0);
    }

    public FlowLayout(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
        this.ˊ = false;
        ॱ(context, attributeSet);
    }

    private void ॱ(Context context, AttributeSet attributeSet) {
        TypedArray obtainStyledAttributes = context.getTheme().obtainStyledAttributes(attributeSet, n.ﾟॱ, 0, 0);
        this.ˋ = obtainStyledAttributes.getDimensionPixelSize(n.ﹺ, 0);
        this.ˎ = obtainStyledAttributes.getDimensionPixelSize(n.ﹶ, 0);
        obtainStyledAttributes.recycle();
    }

    protected void ˏ(int i) {
        this.ˋ = i;
    }

    protected void ˊ(int i) {
        this.ˎ = i;
    }

    protected boolean ˏ() {
        return this.ˊ;
    }

    public void setSingleLine(boolean z) {
        this.ˊ = z;
    }

    protected void onMeasure(int i, int i2) {
        int size = MeasureSpec.getSize(i);
        int mode = MeasureSpec.getMode(i);
        int size2 = MeasureSpec.getSize(i2);
        int mode2 = MeasureSpec.getMode(i2);
        int i3 = (mode == Integer.MIN_VALUE || mode == 1073741824) ? size : ActivityChooserViewAdapter.MAX_ACTIVITY_COUNT_UNLIMITED;
        int paddingLeft = getPaddingLeft();
        int paddingTop = getPaddingTop();
        int paddingRight = i3 - getPaddingRight();
        int i4 = paddingTop;
        int i5 = paddingLeft;
        paddingLeft = paddingTop;
        paddingTop = 0;
        for (int i6 = 0; i6 < getChildCount(); i6++) {
            View childAt = getChildAt(i6);
            if (childAt.getVisibility() != 8) {
                int i7;
                int i8;
                measureChild(childAt, i, i2);
                LayoutParams layoutParams = childAt.getLayoutParams();
                if (layoutParams instanceof MarginLayoutParams) {
                    MarginLayoutParams marginLayoutParams = (MarginLayoutParams) layoutParams;
                    i7 = 0 + marginLayoutParams.leftMargin;
                    i3 = marginLayoutParams.rightMargin + 0;
                    i8 = i7;
                } else {
                    i3 = 0;
                    i8 = 0;
                }
                if ((i5 + i8) + childAt.getMeasuredWidth() > paddingRight && !ˏ()) {
                    i5 = getPaddingLeft();
                    i4 = this.ˋ + paddingLeft;
                }
                paddingLeft = (i5 + i8) + childAt.getMeasuredWidth();
                i7 = childAt.getMeasuredHeight() + i4;
                if (paddingLeft > paddingTop) {
                    paddingTop = paddingLeft;
                }
                i5 += ((i3 + i8) + childAt.getMeasuredWidth()) + this.ˎ;
                paddingLeft = i7;
            }
        }
        setMeasuredDimension(ˊ(size, mode, paddingTop), ˊ(size2, mode2, paddingLeft));
    }

    private static int ˊ(int i, int i2, int i3) {
        switch (i2) {
            case Integer.MIN_VALUE:
                return Math.min(i3, i);
            case 1073741824:
                return i;
            default:
                return i3;
        }
    }

    protected void onLayout(boolean z, int i, int i2, int i3, int i4) {
        if (getChildCount() != 0) {
            Object obj = ViewCompat.ʼ(this) == 1 ? 1 : null;
            int paddingRight = obj != null ? getPaddingRight() : getPaddingLeft();
            int paddingLeft = obj != null ? getPaddingLeft() : getPaddingRight();
            int paddingTop = getPaddingTop();
            int i5 = (i3 - i) - paddingLeft;
            int i6 = paddingTop;
            int i7 = paddingTop;
            int i8 = paddingRight;
            for (paddingTop = 0; paddingTop < getChildCount(); paddingTop++) {
                View childAt = getChildAt(paddingTop);
                if (childAt.getVisibility() != 8) {
                    int ˋ;
                    LayoutParams layoutParams = childAt.getLayoutParams();
                    if (layoutParams instanceof MarginLayoutParams) {
                        MarginLayoutParams marginLayoutParams = (MarginLayoutParams) layoutParams;
                        ˋ = hi.ˋ(marginLayoutParams);
                        paddingLeft = hi.ॱ(marginLayoutParams);
                    } else {
                        paddingLeft = 0;
                        ˋ = 0;
                    }
                    int measuredWidth = (i8 + ˋ) + childAt.getMeasuredWidth();
                    if (!this.ˊ && measuredWidth > i5) {
                        i7 = this.ˋ + i6;
                        i8 = paddingRight;
                    }
                    measuredWidth = childAt.getMeasuredWidth() + (i8 + ˋ);
                    i6 = childAt.getMeasuredHeight() + i7;
                    if (obj != null) {
                        childAt.layout(i5 - measuredWidth, i7, (i5 - i8) - ˋ, i6);
                    } else {
                        childAt.layout(i8 + ˋ, i7, measuredWidth, i6);
                    }
                    i8 += ((paddingLeft + ˋ) + childAt.getMeasuredWidth()) + this.ˎ;
                }
            }
        }
    }
}
